﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Meebey.SmartIrc4net;
using System.Threading; 

namespace IrcYo
{
    public partial class Form1 : Form
    {
        private IrcClient irc = new IrcClient();
        private string server = "irc.quakenet.org";
        private int port = 6669;
        Thread listenthread;
        Channel channel;
        private bool addOpfer = true;
        Opfer op1;

        Opfer op2; 

        List<IrcChannelControl> channelControls = new List<IrcChannelControl>();
        List<IrcDuellControl> duellControls = new List<IrcDuellControl>();

        Dictionary<string, Opfer> userDict = new Dictionary<string, Opfer>(); 


        #region Events

        void OnConnected(object sender, EventArgs e)
        {
            if (this.InvokeRequired) this.BeginInvoke(new EventHandler(OnConnected), new object[] { sender, e });
            else
            {

            }
        }

        void OnChannelMessage(object sender, IrcEventArgs e)
        {
            if (this.InvokeRequired)
            {                
                this.BeginInvoke(new IrcEventHandler(OnChannelMessage), new object[] { sender, e });              
            }
            else
            {

                foreach (IrcChannelControl con in channelControls)
                {
                    if (e.Data.Channel.ToUpper() == con.Channel.ToUpper())
                    {
                        con.OnChannelMessage(e);
                    }
                }
                updateUsers();
                if(userDict.Keys.Contains(e.Data.Nick))
                {
                    if (e.Data.Type == ReceiveType.ChannelMessage)
                    {
                        userDict[e.Data.Nick].addZeile(e.Data.Message);
                    }
                }
                updateUsers(); 
            }

        }

        public void OnJoin(object sender, JoinEventArgs e)
        {
            if (this.InvokeRequired)
            {
                this.BeginInvoke(new JoinEventHandler(OnJoin), new object[] { sender, e });
            }
            else
            {

                foreach (IrcChannelControl con in channelControls)
                {
                    if (e.Channel.ToUpper() == con.Channel.ToUpper())
                    {
                        con.OnJoin(e);
                    }
                }
                updateUsers(); 
            }
        }

        public void OnQueryMessage(object sender, IrcEventArgs e)
        {
            if (this.InvokeRequired)
            {
                this.BeginInvoke(new IrcEventHandler(OnQueryMessage), new object[] { sender, e });
            }
            else
            {
                foreach (IrcDuellControl idc in duellControls)
                {
                    if (idc.Op1Nick == e.Data.Nick || idc.Op2Nick == e.Data.Nick)
                    {
                        idc.OnChat(e); 
                    }
                }
            }
        }

        #endregion

        public Form1()
        {
            InitializeComponent();
            irc.ActiveChannelSyncing = true;
            irc.OnConnected += new EventHandler(OnConnected);
            irc.OnChannelMessage += new IrcEventHandler(OnChannelMessage);
            irc.OnJoin += new JoinEventHandler(OnJoin);
            irc.OnQueryMessage += new IrcEventHandler(OnQueryMessage);
            irc.OnReadLine += new ReadLineEventHandler(OnReadLine);

        }

        void OnReadLine(object sender, ReadLineEventArgs e)
        {
            if (this.InvokeRequired)
            {
                this.BeginInvoke(new ReadLineEventHandler(OnReadLine), new object[] { sender, e });
            }
            else
            {
                listServer.Items.Add(e.Line);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {

        }

        private void listentoirc()
        {
            irc.Login(txtNick.Text, "Sandra", 0, "bye");
            irc.Listen();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            IrcChannelControl tmpControl = new IrcChannelControl(irc, txtChannel.Text);

            channelControls.Add(tmpControl);
            tabChannel.TabPages.Add(txtChannel.Text); 
            tabChannel.TabPages[tabChannel.TabPages.Count-1].Controls.Add(channelControls[channelControls.Count - 1]); 
            channelControls[channelControls.Count - 1].Dock = DockStyle.Fill; 
            channelControls[channelControls.Count - 1].Show();           
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            addDuell();
        }

        private void updateUsers()
        {
            //userDict.Clear(); 
            foreach (IrcChannelControl icc in channelControls)
            {
                foreach (ChannelUser user in icc.Users)
                {
                    if(!userDict.Keys.Contains(user.Nick))
                    {
                        userDict.Add(user.Nick, new Opfer(user)); 
                    }
                }
            }

            listUsers.Items.Clear();
            List<Opfer> tmpsorter = new List<Opfer>(); 
            foreach (Opfer op in userDict.Values)
            {
                tmpsorter.Add(op); 

            }
            tmpsorter.Sort();
            foreach (Opfer op in tmpsorter)
            {
                listUsers.Items.Add(op); 
            }

        }

        private void listUsers_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                Opfer selOp = (Opfer)listUsers.Items[listUsers.SelectedIndex];

                if (addOpfer)
                {
                    txtOpfer1.Text = selOp.Nick;
                    op1 = selOp;
                    addOpfer = false;
                }
                else
                {
                    op2 = selOp;
                    txtOpfer2.Text = selOp.Nick;
                    addOpfer = true;
                }
            }
            catch { }
        }

        private void addDuell()
        {
            IrcDuellControl tmpControl = new IrcDuellControl(irc, op1, op2, txtOper1msg.Text, txtOpfer2msg.Text);
            duellControls.Add(tmpControl);
            tabduell.TabPages.Add(op1.Nick+ " vs "+op2.Nick);
            tabduell.TabPages[tabduell.TabPages.Count - 1].Controls.Add(duellControls[duellControls.Count - 1]);
            duellControls[duellControls.Count - 1].Dock = DockStyle.Fill;
            duellControls[duellControls.Count - 1].Show();  
        }

        private void tabPage2_Click(object sender, EventArgs e)
        {

        }

        private void tabChannel_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void btnConnect_Click(object sender, EventArgs e)
        {
            listenthread = new Thread(listentoirc);
            try
            {
                irc.Connect(txtServer.Text, int.Parse(txtPort.Text));
            }
            catch 
            {
                MessageBox.Show("Verbindung Fehlgeschlagen"); 
            }
            listenthread.Start(); 
        }

    }
}
